package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.utils.CurrentUserHoler;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;


@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    //商机列表
    @Override
    public PageResult<Business> listBusinesses(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusinesses(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    //根据ID查询商机列表
    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }

    //跟进商机
    @Transactional(rollbackFor = Exception.class)//事务管理
    //本来默认只会运行时异常才会回滚，所以这里要加上rollbackFor = Exception.class
    //现在加上这个属性后就会回滚所有异常
    // (事务四大特性:ACID 原子性;一致性;隔离性;持久性)
    @Override
    public void trackBusiness(Business business) {
        //根据ID跟新线索的基本信息
        business.setStatus(3);//此时设置为 '跟进中'//商机状态，1:待分配，2:待跟进，3:跟进中，4:回收，5：转客户
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //添加跟进记录---插入一条数据
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人ID
        businessTrackRecord.setTrackStatus(business.getTrackStatus());//跟进状态:1:接通，2:拒绝，3:无人接听
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }


    //商机转客户 todo   大问题!!!!好好看看!!!!
    @Override
    public void toCustomer(Integer id) {
        //根据ID更新商机信息
        Business business =businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //创建客户信息(根据商机信息拼装客户信息)
        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);//拷贝属性
        customer.setId(null);
        customer.setBusinessId(business.getId());

        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        //插入客户信息
        customerMapper.insert(customer);
    }

    //公海池列表查询

    @Override
    public PageResult<Business> listpoolBusinesses(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listpoolBusinesses(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());

    }
}
